package cn.chenhongchao.zhaobiaopingtai.service.impl;

import cn.chenhongchao.zhaobiaopingtai.common.api.CommonPage;
import cn.chenhongchao.zhaobiaopingtai.constant.BidConstant;
import cn.chenhongchao.zhaobiaopingtai.dto.ReturnBidDTO;
import cn.chenhongchao.zhaobiaopingtai.entity.ReturnBid;
import cn.chenhongchao.zhaobiaopingtai.mapper.ReturnBidMapper;
import cn.chenhongchao.zhaobiaopingtai.service.ReturnBidService;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import javax.annotation.Resource;
import java.util.List;

@Service
public class ReturnBidServiceImpl extends ServiceImpl<ReturnBidMapper, ReturnBid> implements ReturnBidService {

    @Resource
    private ReturnBidMapper returnBidMapper;

    /***
     * @Description  查所有
     * @Date 11:14 2021/8/9
     * @Param []
     * @return java.util.List<cn.chenhongchao.zhaobiaopingtai.entity.ReturnBid>
     **/
    @Override
    public List<ReturnBid> getAllReturnBiDList() {
        List<ReturnBid> returnBiDList = returnBidMapper.getAllReturnBiDList();
        return returnBiDList;
    }

    @Override
    public List<ReturnBid> getReturnBiDListByPage(Integer pageNum, Integer pageSize) {
        pageNum = (pageNum - 1) * pageSize;
        List<ReturnBid> returnBiDList2 = returnBidMapper.getReturnBiDListByPage(pageNum, pageSize);
        return returnBiDList2;
    }

    @Override
    public Long getTotal() {
        return returnBidMapper.getTotal();
    }

    @Override
    public CommonPage<ReturnBid> dynamicQuery(Integer pageNum, Integer pageSize, ReturnBidDTO returnBidDTO) {

        QueryWrapper<ReturnBid> wrapper = new QueryWrapper<>();

        // TODO tenderType 不知道对应哪个字段
//        if (!StringUtils.isEmpty(returnBidDTO.getTenderType())) {
//            wrapper.eq("commCode", returnBidDTO.getTenderType());
//        }1

        if (!StringUtils.isEmpty(returnBidDTO.getTenderNumber())) {
            wrapper.eq("tender_umber", returnBidDTO.getTenderNumber());
        }

        if (!StringUtils.isEmpty(returnBidDTO.getTenderName())) {
            wrapper.like("tender_name", returnBidDTO.getTenderName());
        }

        if (!StringUtils.isEmpty(returnBidDTO.getCommCode())) {
            wrapper.like("comm_code", returnBidDTO.getCommCode());
        }

        // 分页查询
        Page<ReturnBid> page = new Page<>(pageNum, pageSize);
        returnBidMapper.selectPage(page, wrapper);

        // 分装返回需要的类型
        CommonPage<ReturnBid> returnBidCommonPage = new CommonPage<ReturnBid>();
        returnBidCommonPage.setList(page.getRecords());

        Long pages = page.getPages();
        Integer total = Integer.valueOf(pages.toString());
        returnBidCommonPage.setTotalPage(total);

        returnBidCommonPage.setPageSize(pageSize);

//        Long current = page.getCurrent();
//        Integer num = Integer.valueOf(current.toString());
        returnBidCommonPage.setPageNum(pageNum);
        returnBidCommonPage.setTotal(page.getTotal());

        return returnBidCommonPage;
    }

    @Override
    public void getScheduleMax(Integer code) {
        ReturnBid entity = this.getOne(new QueryWrapper<ReturnBid>().eq("tenderNumber", code));
        entity.setTenderScheduleStatus(BidConstant.完成回标);
        this.updateById(entity);
    }
}
